package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.jzweb.domain.vo.JazCoursesChapterAddVo;
import com.jinzhi.jzweb.domain.vo.JazCoursesChapterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazCoursesChapterDao;
import com.jinzhi.jzweb.domain.JazCoursesChapterDO;
import com.jinzhi.jzweb.service.JazCoursesChapterService;
import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * 课程章节表
 * </pre>
 * <small> 2024-05-13 17:02:15 | Long</small>
 */
@Service
public class JazCoursesChapterServiceImpl extends CoreServiceImpl<JazCoursesChapterDao, JazCoursesChapterDO> implements JazCoursesChapterService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCourseChapter(JazCoursesChapterAddVo jazCoursesChapterVo) {
        JazCoursesChapterDO jazCoursesChapterDO = new JazCoursesChapterDO();
        jazCoursesChapterDO.setChapterName(jazCoursesChapterVo.getChapterName());
        jazCoursesChapterDO.setSort(jazCoursesChapterVo.getSort());
        jazCoursesChapterDO.setVideoUrl(jazCoursesChapterVo.getVideoUrl());
        BeanUtils.copyProperties(jazCoursesChapterVo, jazCoursesChapterDO);
        this.insert(jazCoursesChapterDO);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCourseChildChapter(List<JazCoursesChapterAddVo> jazCoursesChapterVo) {
        jazCoursesChapterVo.forEach(jazCoursesChapterAddVo ->{
            JazCoursesChapterDO jazCoursesChapterDO = new JazCoursesChapterDO();
            BeanUtils.copyProperties(jazCoursesChapterAddVo, jazCoursesChapterDO);
            jazCoursesChapterDO.setChapterName(jazCoursesChapterAddVo.getChapterName());
            jazCoursesChapterDO.setSort(jazCoursesChapterAddVo.getSort());
            jazCoursesChapterDO.setVideoUrl(jazCoursesChapterAddVo.getVideoUrl());
            jazCoursesChapterDO.setParentId(jazCoursesChapterDO.getParentId());
            this.insert(jazCoursesChapterDO);
        });
    }

    @Override
    public List<JazCoursesChapterVo> selectCourseChapter(Long courseId) {
        List<JazCoursesChapterVo> voList = new ArrayList<>();
        Wrapper<JazCoursesChapterDO> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("parent_id", courseId);
        queryWrapper.orderBy("sort");
        List<JazCoursesChapterDO> list = this.selectList(queryWrapper);
        for (JazCoursesChapterDO jazCoursesChapterDO : list) {
            JazCoursesChapterVo jazCoursesChapterVo = new JazCoursesChapterVo();
            BeanUtils.copyProperties(jazCoursesChapterDO, jazCoursesChapterVo);
            List<JazCoursesChapterVo> childVos = new ArrayList<>();
            List<JazCoursesChapterDO> childList = this.selectList(new EntityWrapper<JazCoursesChapterDO>().eq("parent_id", jazCoursesChapterDO.getId()).orderBy("sort"));
            if (!list.isEmpty()) {
                for (JazCoursesChapterDO child : childList) {
                    JazCoursesChapterVo vo = new JazCoursesChapterVo();
                    BeanUtils.copyProperties(child, vo);
                    childVos.add(vo);
                }
            }
            jazCoursesChapterVo.setChildChapterVos(childVos);
            voList.add(jazCoursesChapterVo);
        }
        return voList;
    }

    @Override
    public JazCoursesChapterVo selectChildCourse(Long id) {
        JazCoursesChapterDO jazCoursesChapterDO = this.selectById(id);
        JazCoursesChapterVo vo = new JazCoursesChapterVo();
        BeanUtils.copyProperties(jazCoursesChapterDO, vo);
        List<JazCoursesChapterVo> childVos = new ArrayList<>();
        List<JazCoursesChapterDO> childList = this.selectList(new EntityWrapper<JazCoursesChapterDO>().eq("parent_id", jazCoursesChapterDO.getId()).orderBy("sort"));
        for (JazCoursesChapterDO child : childList) {
            JazCoursesChapterVo vo1 = new JazCoursesChapterVo();
            BeanUtils.copyProperties(child, vo1);
            childVos.add(vo);
        }
        vo.setChildChapterVos(childVos);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourseChapter(JazCoursesChapterVo vo) {
        JazCoursesChapterDO jazCoursesChapterDO = this.selectById(vo.getId());
        BeanUtils.copyProperties(vo,jazCoursesChapterDO);

        this.updateById(jazCoursesChapterDO);
    }
}
